﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SymWorld.Model;
using System.Drawing;

namespace SymWorld.Renderer
{
    public class WorldStateRenderer
    {
        private WorldState _worldState;
        public WorldStateRenderer(WorldState worldState)
        {
            _worldState = worldState;
        }
        public void Render(System.Drawing.Graphics graphics)
        {
            float centerX = graphics.VisibleClipBounds.Width / 2f;
            float centerY = graphics.VisibleClipBounds.Height / 2f;
            double zoom = 2d;
            int i =0;
            foreach (ParticleState particleState in _worldState.ParticleStateList)
            {

                graphics.DrawEllipse(System.Drawing.Pens.Black, (float)(particleState.Position.X*zoom) - 3f + centerX, (float)(particleState.Position.Y*zoom) - 3f + centerY, 6f, 6f);
                graphics.DrawString(string.Format("v{0} = {1}",i,particleState.Velocity.Length()),new System.Drawing.Font("Arial",10f),System.Drawing.Brushes.Black,new System.Drawing.PointF(10f,i*20f));
                i++;        
            }

            foreach (ParticleState particleState in _worldState.ParticleStateList)
            {
                int particleStateIndex = _worldState.ParticleStateList.IndexOf(particleState);
                ParticleState historicParticleState = particleState;
                Point prevPoint = new Point();
                int index = 0;
                while (historicParticleState !=null)
                {
                    Point point = new Point((int)(historicParticleState.Position.X * zoom + centerX), (int)(historicParticleState.Position.Y * zoom + centerY));
                    if (index %50  == 0)
                    {
                        if (prevPoint != new Point())
                        graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.FromArgb(particleStateIndex * 16, particleStateIndex * 16, particleStateIndex * 16)),
                        prevPoint, point);
                        prevPoint = point;
                    }
                    historicParticleState = historicParticleState.GetPrevParticleState();
                    index++;
                }

            }

        }
    }
}
